package org.oschina.api.openapi.dao;

import java.util.List;
import org.oschina.api.BaseDAO;
import org.oschina.api.openapi.model.OpenApi;
import org.oschina.api.openapi.vo.OpenApiVo;
import org.springframework.stereotype.Repository;

@Repository
public class OpenApiDAO extends BaseDAO {

	private final String findVo[]={"id","name","url","format","request_type","parameter","`explain`","result","parent","`index`","create_time"};
	
	/**
	 * 返回
	 * @return
	 */
	public List<OpenApiVo> findVo()
	{
		return getQueryHelper().query_cache(findVo, null, new String[]{"ORDER BY `index`"}, null, OpenApiVo.class, -1, -1);
	}
	
	/**
	 * 创建
	 * @param openApi
	 * @return
	 */
	public OpenApi create(OpenApi openApi)
	{
		getQueryHelper().insert_cache(new String[]{"id","name","url","format","request_type","parameter","`explain`","result","parent","`index`","create_time"}, 
				new Object[]{openApi.getId(),openApi.getName(),openApi.getUrl(),openApi.getFormat(),openApi.getRequest_type(),openApi.getParameter(),
					openApi.getExplain(),openApi.getResult(),openApi.getParent(),openApi.getIndex(),openApi.getCreate_time()}, OpenApiVo.class);
		return openApi;
	}
	
	/**
	 * 更新
	 * @param openApi
	 * @return
	 */
	public int update(OpenApi openApi)
	{
		return (int) getQueryHelper().update_cache(new String[]{"name=?","url=?","format=?","request_type=?","parameter=?","`explain`=?","result=?","parent=?","`index`=?"}, 
				new String[]{"id=?"},
				new Object[]{openApi.getName(),openApi.getUrl(),openApi.getFormat(),openApi.getRequest_type(),openApi.getParameter(),
					openApi.getExplain(),openApi.getResult(),openApi.getParent(),openApi.getIndex(),openApi.getId()}, OpenApiVo.class);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int remove(int id)
	{
		return (int) getQueryHelper().delete_cache(new String[]{"id=?"}, new Object[]{id}, OpenApiVo.class);
	}
}